//
//  UIImage+CycleImage.m
//  todayHappyTalk
//
//  Created by qu on 2017/1/3.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "UIImage+CycleImage.h"

@implementation UIImage (CycleImage)
- (UIImage *)cycleImage
{
    // 开启图形上下文
    UIGraphicsBeginImageContext(self.size);
    
    // 获得上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 矩形框
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    
    // 画一个圆
    CGContextAddEllipseInRect(context, rect);
    
    // 裁剪成上面画的图形形状
    CGContextClip(context);
    
    // 将image绘制到刚才的圆形上
    //    UIImage *image = [UIImage imageNamed:@"1.jpg"];
    //    [image drawInRect:rect];
    [self drawInRect:rect];
    
    // 获得上下文的图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    
    return newImage;
}


+ (UIImage *)cycleImageNamed:(NSString *)imgName
{
    return [[UIImage imageNamed:imgName] cycleImage];
}
@end
